﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CodeGenerateEngine.Common
{
    internal class IOUtils
    {
        /// <summary>
        /// 按行读取文件所有文本 UTF8
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string ReadContent(string filePath)
        {
            return ReadContent(filePath, new System.Text.UTF8Encoding(false));
        }

        /// <summary>
        /// 按行读取文件所有文本，制定编码格式
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string ReadContent(string filePath, Encoding encoding)
        {
            if (File.Exists(filePath))
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    using (StreamReader sr = new StreamReader(fs, encoding))
                    {
                        return sr.ReadToEnd();
                    }
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 写文本文件
        /// </summary>
        /// <param name="Str"></param>
        public static void WriteTxt(string Str, string FilePath)
        {
            WriteTxt(Str, FilePath, true);   //默认追加文本
        }

        /// <summary>
        /// 写文本文件
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="isAppend">是否追加</param>
        public static void WriteTxt(string Str, string FilePath, bool isAppend)
        {
            WriteTxt(Str, FilePath, isAppend, Encoding.UTF8);   //默认UTF8保存
        }

        /// <summary>
        /// 写文本文件
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="isAppend">是否追加</param>
        public static void WriteTxt(string Str, string FilePath, bool isAppend, Encoding strEncode)
        {
            FileStream fsapp;
            if (File.Exists(FilePath))
            {
                if (isAppend)
                    fsapp = new FileStream(@FilePath, FileMode.Append, FileAccess.Write);
                else
                {
                    fsapp = new FileStream(@FilePath, FileMode.Open, FileAccess.Write);
                    fsapp.SetLength(0);   //清空文件内容
                }
            }
            else
            {
                fsapp = new FileStream(@FilePath, FileMode.Create);
            }
            StreamWriter sw = new StreamWriter(fsapp, strEncode);
            sw.WriteLine(Str);
            sw.Flush();
            sw.Close();
            fsapp.Close();
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public static bool CreatDirectory(string directory)
        {
            //判断文件夹是否存在
            if (Directory.Exists(directory))
            {
                return true;
            }
            try
            {
                Directory.CreateDirectory(directory);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
        }
    }
}
